package io.chain.ko.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import io.chain.ko.service.JWTTokenService;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

@Service
@RefreshScope
public class JWTTokenServiceImpl implements JWTTokenService{
	
	@Value("token.secretKey")
	private String secretKey;
	
	@Override
	public String makeToken(String username) {
		Map<String,Object> claims=new HashMap<>();
		claims.put("sub", username);
		claims.put("created", new Date());
		String token=Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secretKey).compact();
		return token;
	}

	@Override
	public String parseToken(String token) {		
		String username=Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody().getSubject();
		return username;
	}

}
